<template>
  <div>
    <div class="el-row--flex is-justify-space-between">
      <div>
        <el-button size="small" @click="handleButtonCommend('OrgDelete')">
          组织删除
        </el-button>
        <el-button size="small" @click="handleButtonCommend('OrgSetMail')">
          设置邮箱
        </el-button>

        <el-button size="small" @click="handleButtonCommend('OrgUnDisable')">
          组织启用
        </el-button>
        <el-button
          size="small"
          style="margin-left: 0;"
          @click="handleButtonCommend('OrgDisable')"
        >
          组织禁用
        </el-button>
      </div>
      <div>
        <el-button size="small" @click="$refs.CreateOrganization.show()">
          组织创建
        </el-button>
      </div>
    </div>

    <el-table
      class="mt10"
      :data="tableData"
      :header-cell-style="{ background: '#f5f5f5' }"
      size="mini"
    >
      <el-table-column label="选择" width="60">
        <template slot-scope="scope">
          <el-radio
            v-model="tableSelect"
            :label="scope.row.id"
            style="margin-left: 10px;"
            @change="getTemplateRow(scope.row.id)"
          >
            &nbsp;
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column label="组织 ID" prop="id" sortable />
      <el-table-column label="组织名" prop="name" />
      <el-table-column label="组织序列号" prop="sn" />
      <el-table-column label="创建时间" prop="create_time" sortable />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <div
            :class="[
              'status',
              scope.row.active === '可用' ? 'success' : 'fail'
            ]"
          ></div>
        </template>
      </el-table-column>
    </el-table>

    <div class="mt10 el-row--flex is-justify-end">
      <el-pagination
        background
        :current-page="page"
        layout="total, prev, pager, next"
        :page-size="limit"
        :page-sizes="[100, 200, 300, 400]"
        small
        :total="count"
        @current-change="handleCurrentChangePage"
      />
    </div>
    <create-organization ref="CreateOrganization" @change="loadingOrgList" />
  </div>
</template>

<script>
import org from "@/api/org";
import org_getall from "@/utils/mixin/org_getall";
import CreateOrganization from "@/views/organization/toast/createOrganization";
import radioTable from "@/utils/mixin/radioTable";
import checkSelectTable from "@/utils/mixin/checkSelectTable";
export default {
  name: "Manage",
  components: { CreateOrganization },
  mixins: [org_getall, radioTable, checkSelectTable],
  data: () => {
    return {
      search: {
        org_id: ""
      },
      tableData: [],
      limit: 20,
      page: 1,
      count: 0
    };
  },
  mounted() {
    this.loadingData();
  },
  methods: {
    getData() {},
    handleOrgDelete({ id }) {
      org
        .org_delete({
          org_id: id
        })
        .then(res => {
          this.$message.success(res.data.message);
          this.loadingOrgList();
        });
    },
    handleOrgUnDisable({ id }) {
      org
        .org_undisable({
          org_id: id
        })
        .then(res => {
          this.$message.success(res.data.message);
          this.loadingOrgList();
        });
    },
    handleOrgDisable({ id }) {
      org
        .org_disable({
          org_id: id
        })
        .then(res => {
          this.$message.success(res.data.message);
          this.loadingOrgList();
        });
    },
    handleOrgSetMail({ id }) {
      this.$prompt("请输入邮箱", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
        inputErrorMessage: "邮箱格式不正确"
      })
        .then(({ value }) => {
          return org.org_setemail({
            org_id: id,
            email: value
          });
        })
        .then(res => {
          this.$message.success(res.data.message);
          this.loadingOrgList();
        });
    },

    handleCurrentChangePage(val) {
      this.page = val;
      this.getData();
    },
    async loadingData() {
      await this.loadingOrgGetall();
      await this.loadingOrgList();
    },
    loadingOrgList() {
      return org
        .org_getall({
          page: this.page,
          limit: this.limit
        })
        .then(res => {
          this.count = res.data.count;
          this.tableData = res.data.orgs;
        });
    },

    handleButtonCommend(module) {
      let data = {};
      this.is_check_table("id") &&
        (data = this.is_get_select_data()) &&
        this[`handle${module}`](data);
    },
    setMail() {
      this.$prompt("请输入邮箱", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
        inputErrorMessage: "邮箱格式不正确"
      })
        // eslint-disable-next-line no-unused-vars
        .then(({ value }) => {})
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入"
          });
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.status {
  width: 15px;
  height: 15px;
  border-radius: 100px;
}
.success {
  background-color: $--color-success;
}
.fail {
  background-color: $--color-danger;
}
</style>
